<?php
namespace app\home\pointscontroller;
use think\Request;
use phpException\SuccessException;
use app\home\validate\Indexv;
use app\index\model\User;
use phpException\ErrorException;	
class Index
{
	//路由 /think
	public function index()
	{
		try{
			//判断是否post请求
			if(request()->isPost()){
				$arr = request()->param();
				//调用验证器
				$validate = new \app\home\validate\Indexv;
				//抛出异常
				if (!$validate->check($arr)) {
            		throw new ErrorException($validate->getError(),412);
        		}

				$where = [
					['nickname','eq',$arr['name']],
					['password','eq',md5($arr['password'])],
				];

				$user = new User;
				$data = $user->find($where);
				if (empty($data)) {
					throw new ErrorException('账号密码不正确',412);
				}else{
					$token = create_token();
					\Cache::set($token,$data,3600);
					throw new SuccessException('登录成功',200,['token'=>$token]);
				}
			}else{
				throw new ErrorException('登录出错');
			}
		}catch(Exception $e){
			throw new ErrorException($e);
		}
	}
	
}